package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.exception.BaseException;
import com.itheima.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/member")
@Api(tags = "登录相关")
public class MemberController {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login (@RequestBody User user) {
        if (!user.getValidateCode().equals(redisTemplate.opsForValue().get(user.getTelephone()))) {
            throw new BaseException(MessageConstant.VALIDATECODE_ERROR);
        }
        return Result.success();
    }
}
